﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessDA;
using CognisessService;
using CognisessWeb.Models;

namespace CognisessWeb.Controllers
{
    public class ContentInfoController : Controller
    {
        AccountBLL accountBLL = new AccountBLL();
        //
        // GET: /ContentInfo/

        public ActionResult Index()
        {
            List<ContentInfoModel> contentInfos = new List<ContentInfoModel>();
            List<ContentInfo> listContentInfo = new List<ContentInfo>();
            listContentInfo = accountBLL.GetContentInfo(new ContentInfo { });
            foreach (var item in listContentInfo)
            {
                ContentInfoModel contentInfo = new ContentInfoModel();
                contentInfo.Id = item.Id;
                contentInfo.MenuContent = item.MenuContent;
                contentInfo.NavigationId = item.NavigationId;
                contentInfo.DateTime = DateTime.Parse(item.DateTime.ToString());
                contentInfo.Navigations = new List<NavigationModel> { };
                List<Navigation> navigations = new List<Navigation>();
                navigations = accountBLL.GetNavigation(new Navigation { Id = contentInfo.NavigationId });
                for (int i = 0; i < navigations.Count; i++)
                {
                    contentInfo.Navigations.Add(new NavigationModel { Id = contentInfo.NavigationId, TitleName = navigations[i].TitleName ,ParentId = navigations[i].ParentId,Type=navigations[i].Type,Sort=navigations[i].Sort,DateTime= DateTime.Parse(navigations[i].DateTime.ToString()),Link=navigations[i].Link });
                }
                contentInfos.Add(contentInfo);
            }
            return View(contentInfos);
        }

        //
        // GET: /ContentInfo/Details/5

        public ActionResult Details(int id)
        {
            List<ContentInfoModel> listModel = new List<ContentInfoModel>();
            List<ContentInfo> listContentInfo = new List<ContentInfo>();
            ContentInfo cotent = new ContentInfo();
            cotent.Id = id;
            listContentInfo = accountBLL.GetContentInfo(cotent);
            ContentInfoModel model = new ContentInfoModel();
            foreach (var item in listContentInfo)
            {
                model.Id = item.Id;
                model.NavigationId = item.NavigationId;
                model.MenuContent = item.MenuContent;
                model.DateTime = DateTime.Parse(item.DateTime.ToString());
                model.Navigations = new List<NavigationModel> { };
                List<Navigation> navigations = new List<Navigation>();
                navigations = accountBLL.GetNavigation(new Navigation { Id = model.NavigationId });
                for (int i = 0; i < navigations.Count; i++)
                {
                    model.Navigations.Add(new NavigationModel { Id = model.NavigationId, TitleName = navigations[i].TitleName, ParentId = navigations[i].ParentId, Type = navigations[i].Type, Sort = navigations[i].Sort, DateTime = DateTime.Parse(navigations[i].DateTime.ToString()), Link = navigations[i].Link });
                }
                
            }
            return View(model);
        }

        //
        // GET: /ContentInfo/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ContentInfo/Create

        [HttpPost]
        public ActionResult Create(ContentInfo collection)
        {
            try
            {
                // TODO: Add insert logic here
                List<ContentInfo> list = new List<ContentInfo>();
                list.Add(collection);
                bool a = accountBLL.AddContentInfo(list);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ContentInfo/Edit/5
 
        public ActionResult Edit(int id)
        {
            List<ContentInfoModel> listmodel = new List<ContentInfoModel>();
            List<ContentInfo> listContent = new List<ContentInfo>();
            ContentInfo content = new ContentInfo();
            content.Id = id;
            listContent = accountBLL.GetContentInfo(content);
            ContentInfoModel model = new ContentInfoModel();
            foreach (var item in listContent)
            {
                model.Id = item.Id;
                model.NavigationId = item.NavigationId;
                model.MenuContent = item.MenuContent;
                model.DateTime = DateTime.Parse(item.DateTime.ToString());
                model.Navigations = new List<NavigationModel> { };
                List<Navigation> navigations = new List<Navigation>();
                navigations = accountBLL.GetNavigation(new Navigation { Id = model.NavigationId });
                for (int i = 0; i < navigations.Count; i++)
                {
                    model.Navigations.Add(new NavigationModel { Id = model.NavigationId, TitleName = navigations[i].TitleName, ParentId = navigations[i].ParentId, Type = navigations[i].Type, Sort = navigations[i].Sort, DateTime = DateTime.Parse(navigations[i].DateTime.ToString()), Link = navigations[i].Link });
                }
            }
            return View(model);
        }

        //
        // POST: /ContentInfo/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ContentInfo collection)
        {
            try
            {
                // TODO: Add update logic here

                List<ContentInfo> list = new List<ContentInfo>();
                list.Add(collection);
                accountBLL.UpdataContentInfo(list);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ContentInfo/Delete/5
 
        public ActionResult Delete(int id)
        {
            accountBLL.DeleteComment(id);
            return View();
        }

        //
        // POST: /ContentInfo/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                accountBLL.DeleteComment(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
